import json
import random
class useragentget:
    def __init__(self,BrowsersName='chrome'):
        with open(r'python\爬虫\UseragentGet\useragent.json', encoding='utf-8') as f:
            self.userAgentDict=json.loads(f.read())

        self.BrowsersName=BrowsersName
        if BrowsersName not in self.userAgentDict['browsers'].keys():
            raise RuntimeError("输入的浏览器名称有错 可供选择的值为\n['chrome', 'opera', 'firefox', 'internetexplorer', 'safari']")
        else:
            self.userAgentList=self.userAgentDict['browsers'][BrowsersName]
    def randUseragentget(self):
        return random.choice(self.userAgentList)

if __name__ == '__main__':
    useragent=useragentget('opera')
    # print(useragent.userAgentList)
    # print(useragent.userAgentDict['browsers'].keys())
    print(useragent.randUseragentget())